package com.ruoyi.web.controller.monitor;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.session.SaTerminalInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysUserOnline;
import com.ruoyi.system.service.ISysUserOnlineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 在线用户监控
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/online")
public class SysUserOnlineController extends BaseController {
    @Autowired
    private ISysUserOnlineService userOnlineService;

//    @Autowired
//    private RedisCache redisCache;

    @SaCheckPermission("monitor:online:list")
    @GetMapping("/list")
    public TableDataInfo<SysUserOnline> list(String ipaddr, String userName
//            , Integer pageNum, Integer pageSize
    ) {
        List<SysUserOnline> userOnlineList = new ArrayList<>();

        // 获取所有已登录的会话id
        List<String> sessionIds = StpUtil.searchSessionId("", 0, -1, false);
//        for (int i = (pageNum - 1) * pageSize, len = Math.min(pageSize + i, sessionIds.size()); i < len; i++) {
        for (int i = 0; i < sessionIds.size(); i++) {
            // 根据会话id，查询对应的 SaSession 对象，此处一个 SaSession 对象即代表一个登录的账号
            SaSession session = StpUtil.getSessionBySessionId(sessionIds.get(i));
            // 查询这个账号都在哪些设备登录了，依据上面的示例，账号A 的 SaTerminalInfo 数量是 3，账号B 的 SaTerminalInfo 数量是 2
            List<SaTerminalInfo> terminalList = session.terminalListCopy();
            for (SaTerminalInfo saTerminalInfo : terminalList) {
                String tokenValue = saTerminalInfo.getTokenValue();
                LoginUser user = (LoginUser) StpUtil.getTokenSessionByToken(tokenValue).get(UserConstants.LOGIN_USER);
                if (Objects.isNull(user)) {
                    continue;
                }
                user.setToken(StrUtil.toString(session.getLoginId()));
                if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
                    userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
                } else if (StringUtils.isNotEmpty(ipaddr)) {
                    userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
                } else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser())) {
                    userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
                } else {
                    userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
                }
            }
        }

//        Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
//        List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
//        for (String key : keys) {
//            LoginUser user = redisCache.getCacheObject(key);
//            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
//                userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
//            } else if (StringUtils.isNotEmpty(ipaddr)) {
//                userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
//            } else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser())) {
//                userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
//            } else {
//                userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
//            }
//        }
        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton(null));

        TableDataInfo<SysUserOnline> tableDataInfo = getDataTable(userOnlineList);
        tableDataInfo.setTotal(sessionIds.size());
        return tableDataInfo;
    }

    /**
     * 强退用户
     */
    @SaCheckPermission("monitor:online:forceLogout")
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/{tokenId}")
    public R<Void> forceLogout(@PathVariable String tokenId) {
        StpUtil.logout(Long.valueOf(tokenId));
//        redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
        return R.ok();
    }
}
